﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMKB_Repository;
using PMKB_Web.Models;
using ExceptionUtils;

namespace PMKB_Web.Controllers
{
    public partial class LinkController : Controller
    {
        public virtual ActionResult Index(int? pageNumber, int? pageSize,string Search)
        {
            
            pageNumber = pageNumber ?? 1;
            pageSize = pageSize ?? 10;
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            clsLoadLinks ll = new clsLoadLinks();
            ll.ConnectionName = SiteUtils.ConnectionString;
            ll.Search = Search;
            ll.LoadLinks(pageNumber.Value,pageSize.Value,"",true);


            return View(ll);
        }

       
        public virtual ActionResult AddNewItem()
        {
            LinkAddOrEditMVC link = new LinkAddOrEditMVC();
            link.ConnectionName = SiteUtils.ConnectionString;
            link.LoadTags();
            return View(link);
        }
        [HttpPost]
        [ValidateInput(false)]        
        public virtual ActionResult AddNewItem(
        [Bind(Exclude = "link.LinkID")]
            LinkAddOrEditMVC  add)
        {
            try
            {

                add.ConnectionName = SiteUtils.ConnectionString;
                if (ModelState.IsValid)
                {
                    add.SaveNew();
                    return RedirectToAction(MVC.Link.Index());
                }
            }
            catch (AggregateException aggrexp)
            {
                
                foreach (var exp in aggrexp.InnerExceptions)
                {
                    ExceptionProperty arg = exp as ExceptionProperty;
                    if (arg != null)
                    {
                        ModelState.AddModelError("link."+arg.Property, arg.Message);
                    }
                    else 
                    { 
                        ModelState.AddModelError("", exp.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                var errormes = string.Format(Resources.Resources.ErrorAdd, (ex.InnerException ?? ex).Message);
                ModelState.AddModelError("", errormes);


            }
            add.LoadTags();
            return View(add);
        }

        [HttpPost]
        public virtual JsonResult Delete(long id)
        {
            try
            {


                LinkAddOrEditMVC link = new LinkAddOrEditMVC();
                link.ConnectionName = SiteUtils.ConnectionString;
                link.Delete(id,common.UserFind.UserName);
                return Json(new { ok = true, message = "" });

            }
            catch (Exception ex)
            {
                //TODO: log
                var errormsg = string.Format(Resources.Resources.ErrorDel, ex.Message);
                return Json(new { ok = false, errormsg });
            }

            
        }
        public virtual ActionResult Edit(long id)
        {
            LinkAddOrEditMVC link = new LinkAddOrEditMVC();
            link.ConnectionName = SiteUtils.ConnectionString;
            link.LoadLink(id);
            link.LoadTags();
            return View(link);
        }
        [HttpPost]
        [ValidateInput(false)] 
        public virtual ActionResult Edit(
             [Bind(Exclude = "link.LinkCreationDate, link.CreationUserID")]
            LinkAddOrEditMVC edit)
        {

            edit.ConnectionName = SiteUtils.ConnectionString;
            edit.Save();
            return RedirectToAction(MVC.Link.Index());
        }

    }
}
